<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/7/4
 * Time: 9:37
 */

namespace app\common\controller;


use app\common\model\Merchant;
use app\common\model\PaymentAd;
use think\Controller;

class Adverput extends Controller
{

  public function getmachadver($mid){
      $Merchant = new Merchant();
      $agentid = $Merchant->where(['id'=>$mid])->value('agent');
      $PaymentAd = new PaymentAd();
      $map['start_time'] = ['lt',time()];
      $map['end_time'] = ['gt',time()];
      $map['agent_id'] = $agentid;
      $map['statu'] = 1;
      $list = $PaymentAd->where($map)->field('id,put_in_mid')->select();
      if ($list){
          $sid = [];
          foreach ($list as $k => $v) {
              $arrid = explode(',', $v['put_in_mid']);//将投放商户id转换为数组
              $arrid2 = in_array($mid, $arrid);
              if ($arrid2) {
                  $sid[]=$v['id'];
              }
          }
          if (!empty($sid)) {
              $adid = array_rand($sid);
              if ($adid !== '') {
                  $data['code'] = 0;
                  $data['message'] = 'success';
                  $data['data'] = $PaymentAd->where(['id' => $sid[$adid]])->find();
                  $PaymentAd->where(['id' => $sid[$adid]])->setInc('view');
              } else {
                  $data['code'] = 3;
                  $data['message'] = 'error';
                  $data['data'] = '';
              }
          }else{
              $data['code'] = 2;
              $data['message'] = 'error';
              $data['data'] = '';
          }
      }else{
          $data['code'] = 1;
          $data['message'] = 'error';
          $data['data'] = '';
      }
        return $data;
  }

}